package cn.iocoder.yudao.adminserver.modules.live.service.projectPlanEvent.impl;

import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlanEvent.vo.LiveProjectPlanEventCreateReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlanEvent.vo.LiveProjectPlanEventExportReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlanEvent.vo.LiveProjectPlanEventPageReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlanEvent.vo.LiveProjectPlanEventUpdateReqVO;
import cn.iocoder.yudao.adminserver.modules.live.convert.projectPlanEvent.LiveProjectPlanEventConvert;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.projectPlanEvent.LiveProjectPlanEventDO;
import cn.iocoder.yudao.adminserver.modules.live.dal.mysql.projectPlanEvent.LiveProjectPlanEventMapper;
import cn.iocoder.yudao.adminserver.modules.live.service.projectPlanEvent.LiveProjectPlanEventService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.adminserver.modules.tool.enums.ToolErrorCodeConstants.PROJECT_PLAN_EVENT_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 项目阶段事项 Service 实现类
 *
 * @author 芋艿
 */
@Service
@Validated
public class LiveProjectPlanEventServiceImpl implements LiveProjectPlanEventService {

    @Resource
    private LiveProjectPlanEventMapper projectPlanEventMapper;

    @Override
    public Long createProjectPlanEvent(LiveProjectPlanEventCreateReqVO createReqVO) {
        // 插入
        LiveProjectPlanEventDO projectPlanEvent = LiveProjectPlanEventConvert.INSTANCE.convert(createReqVO);
        projectPlanEventMapper.insert(projectPlanEvent);
        // 返回
        return projectPlanEvent.getId();
    }

    @Override
    public void updateProjectPlanEvent(LiveProjectPlanEventUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProjectPlanEventExists(updateReqVO.getId());
        // 更新
        LiveProjectPlanEventDO updateObj = LiveProjectPlanEventConvert.INSTANCE.convert(updateReqVO);
        projectPlanEventMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectPlanEvent(Long id) {
        // 校验存在
        this.validateProjectPlanEventExists(id);
        // 删除
        projectPlanEventMapper.deleteById(id);
    }

    private void validateProjectPlanEventExists(Long id) {
        if (projectPlanEventMapper.selectById(id) == null) {
            throw exception(PROJECT_PLAN_EVENT_NOT_EXISTS);
        }
    }

    @Override
    public LiveProjectPlanEventDO getProjectPlanEvent(Long id) {
        return projectPlanEventMapper.selectById(id);
    }

    @Override
    public List<LiveProjectPlanEventDO> getProjectPlanEventList(Collection<Long> ids) {
        return projectPlanEventMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LiveProjectPlanEventDO> getProjectPlanEventPage(LiveProjectPlanEventPageReqVO pageReqVO) {
        return projectPlanEventMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LiveProjectPlanEventDO> getProjectPlanEventList(LiveProjectPlanEventExportReqVO exportReqVO) {
        return projectPlanEventMapper.selectList(exportReqVO);
    }

}
